const wx2my = require('../../../wx2my');
const Behavior = require('../../../Behavior');
var app = getApp();
var util = require("../../utils/util.js");
var base = require('./../../../config.js');
Page({
  data: {
    page: 1,
    hotel: [],
    timg: !1,
    recomed: 2,
    day1: '',
    day2: '',
    imgUrl:''
  },
  onLoad: function (t) {
    my.getLocation({
      success(t) {
        my.hideLoading();
        wx2my.setStorageSync("latitude", t.latitude)
        wx2my.setStorageSync("longitude", t.longitude)
        
      },
      fail() {
        my.hideLoading();
      },
    })
    // t={nearby: "0"}
    console.log(t)
    wx2my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });
    var e = this;
    e.setData({
      day1: wx2my.getStorageSync(["day1"]).data.slice(5, 10),
      day2: wx2my.getStorageSync(["day2"]).data.slice(5, 10),
      color: wx2my.getStorageSync(["platform"]).data.color,
      start: app.util.time(),
      end: app.util.addDate(app.util.time(), 28)
    });
    1 == t.nearby ? e.setData({
      nearby: t.nearby,
      Recommend: 2,
      price_sorting: 0
    }) : e.setData({
      nearby: 0,
      Recommend: 1,
      price_sorting: 0
    });
    var a = wx2my.getStorageSync(["platform"]).data,
        o = wx2my.getStorageSync(["imgUrl1"]).data;
    e.setData({
      platform: a,
      imgUrl: o,
      lat1: wx2my.getStorageSync(["latitude"]).data,
      lng1: wx2my.getStorageSync(["longitude"]).data
    })
    2 == location && wx2my.showLoading({
      title: "正在加载",
      complete: function () {},
      fail: function () {}
    }), e.refresh(), e.date();
  },

  date: function (t) {
    var e = wx2my.getStorageSync(["day1"]).data,
        a = wx2my.getStorageSync(["day2"]).data,
        o = wx2my.getStorageSync(["day"]).data,
        r = app.util.time();

    if (console.log(n), wx2my.setStorageSync("datein", n), "" == e) {
      var n = app.util.time();
      wx2my.setStorageSync("datein", n);
    } else if (e < r) n = r;else n = e;

    if ("" == a) var i = app.util.addDate(r, 1);else {
      var s = app.util.addDate(r, 1);
      if (a < s) i = s;else i = a;
    }
    o = app.util.day(i, n);
    wx2my.setStorageSync("day1", n), wx2my.setStorageSync("day2", i), wx2my.setStorageSync("day", o), this.setData({
      datein: n,
      dateout: i,
      time: o,
      current_date: n
    });
  },
  refresh: function (t) {
    var s = this,
        e = wx2my.getStorageSync(["imgUrl1"]).data;
    s.setData({
      imgUrl: e
    });
    console.log(wx2my.getStorageSync(['day1']).data);
    var c = s.data.page,
        l = s.data.hotel,
        g = s.data.Recommend,
        d = s.data.price_sorting,
        p = s.data.nearby;
    wx2my.newRequest({
        url: base.siteroot , // 目标服务器url
        data: {
          c:'entry',
          a:'wxapp',
          do:'JdList',
          m:'zh_jdgjb',
          page: c,
          size: 7
        },
        success: (t) => {
          console.log('res..@@@@@@@@@@@@')
          console.log(t)
          if (console.log(t), 0 < t.data.length) {
            s.setData({
              page: c + 1,
              none_more: !1
            })
            l = l.concat(t.data);
            console.log(s.data.lat1);
            console.log(s.data.lng1);

            for (var e = 0; e < l.length; e++) {
              var a = l[e].coordinates.split(",");
              
              console.log('aaaaaaaaaaaaaaaaaaaa')
              console.log(a)
              l[e].lat2 = a[0];
              l[e].lng2 = a[1];
              console.log(l)
              var o = s.data.lat1,
                  r = s.data.lng1,
                  n = Number(a[0]),
                  i = Number(a[1]);
              l[e].distance = app.util.location(o, n, r, i);
            }

            1 == g ? s.setData({
              hotel: l,
              timg: !0
            }) : (1 == d && s.setData({
              hotel: l.sort(app.sort_price_order),
              timg: !0
            }), 2 == d && s.setData({
              hotel: l.sort(app.sort_price_Reverse),
              timg: !0
            }), 1 == p && s.setData({
              hotel: l.sort(app.sort_distance_order),
              timg: !0
            }), 2 == p && s.setData({
              hotel: l.sort(app.sort_distance_Reverse),
              timg: !0
            }));
          } else s.setData({
            none_more: !0,
            timg: !0
          });
        },
        fail: (err) => {
          console.log(err)
        }
      }); 
  },
  recomed: function () {
    console.log('酒店地理地位')
    var e = this;
    e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      recomed: 1
    });
    app.util.request({
      url: "entry/wxapp/ScoreSort",
      cachetime: "0",
      data: {
        i: base.acid,
        c: 'entry',
        a: 'wxapp',
        m: 'zh_jdgjb',
        size: 7,
        time: wx2my.getStorageSync(['day1']).data
      },
      success: function (t) {
        var o = e.data.lat1,
            r = e.data.lng1;
        t.data.forEach(function (item, index) {
          console.log(index);
          var a = item.coordinates.split(",");
          t.data[index].lat2 = a[0];
          t.data[index].lng2 = a[1];
          t.data[index].distance = app.util.location(o, Number(a[0]), r, Number(a[1]));
        });
        e.setData({
          hotel: t.data
        });
        console.log(e.data.hotel)
      }
    });
  },
  Recommend: function (t) {
    var e = this;
    e.data.Recommend;
    e.setData({
      price_sorting: 0,
      Recommend: 1,
      nearby: 0,
      page: 1,
      hotel: [],
      recomed: 2
    }), e.refresh();
  },
  price_sorting: function (t) {
    var e = this,
        a = e.data.price_sorting;
    e.data.hotel;
    0 == a ? (e.setData({
      price_sorting: 2,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      timg: !1,
      recomed: 2
    }), e.refresh()) : 1 == a ? (e.setData({
      price_sorting: 2,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh()) : 2 == a && (e.setData({
      price_sorting: 1,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh());
  },
  nearby: function (t) {
    var e = this,
        a = e.data.nearby;
    e.data.hotel_b;
    0 == a ? (e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 2,
      page: 1,
      hotel: [],
      timg: !1,
      recomed: 2
    }), e.refresh()) : 1 == a ? (e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 2,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh()) : 2 == a && (e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 1,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh());
  },
  bindDateChange1: function (t) {
    console.log("picker发送选择改变，携带值为", t.detail.value);
    var e = t.detail.value,
        a = this.data.dateout,
        o = (this.data.current_date, app.getTime2Time(a, e));
    wx2my.setStorageSync("day1", e), wx2my.setStorageSync("day2", a), wx2my.setStorageSync("day", o), this.setData({
      datein: t.detail.value,
      time: o
    });
  },
  bindDateChange2: function (t) {
    console.log("picker发送选择改变，携带值为", t.detail.value);
    var e = this.data.datein,
        a = t.detail.value;
    console.log(e + "+" + a);
    var o = app.getTime2Time(a, e);
    wx2my.setStorageSync("day1", e), wx2my.setStorageSync("day2", a), wx2my.setStorageSync("day", o), this.setData({
      dateout: t.detail.value,
      time: o
    });
  },
  conlist: function (t) {
    console.log(t), wx2my.navigateTo({
      url: "hotel_info?hotel_id=" + t.currentTarget.dataset.id
    });
  },
  search: function (t) {
    wx2my.navigateTo({
      url: "../index/search"
    });
  },
  onReady: function () {},
  onShow: function () {
    console.log("页面显示");
    console.log(this.hotel);
    this.date();
  },
  onHide: function () {},
  onUnload: function () {
    console.log('TM卸载了');
    this.setData({
      location: 2
    });
  },
  onPageScroll: function (e) {
    console.log(e);
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      hotel: [],
      nearby: 0,
      Recommend: 1,
      price_sorting: 0
    }), this.refresh(), wx, wx2my.stopPullDownRefresh();
  },
  onReachBottom: function () {
    console.log('TM到底了'); // this.setData({
    //   location: 2
    // });

    this.setData({
      timg: !1
    }), this.refresh();
  }
});